package com.xexun.gpstracker.baby.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.*;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.nfc.Tag;
import android.os.*;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;

import com.xexun.gpstracker.MainActivity;
import com.xexun.gpstracker.baby.MyPullUpListView;
import com.xexun.gpstracker.baby.mytree.Warning;
import com.xexun.gpstracker.baby.mytree.WarningListAdapter;
import com.xexun.gpstracker.badgenumberlibrary.BadgeNumberManager;
import com.xexun.gpstracker.badgenumberlibrary.MobileBrand;
import com.xexun.gpstracker.dialog.WizardAlertDialog;
import com.xexun.gpstracker.ui.LoginActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.simple.eventbus.Subscriber;
import org.xutils.common.Callback;
import org.xutils.common.util.KeyValue;
import org.xutils.common.util.LogUtil;
import org.xutils.db.sqlite.SqlInfo;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;

import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.baby.adapter.MessageViewAdapter;
import com.xexun.gpstracker.base.BaseActivity;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.AudioRecorder;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.common.MyConstants;
import com.xexun.gpstracker.component.MessageSender;
import com.xexun.gpstracker.fix.grant.PermissionsManager;
import com.xexun.gpstracker.fix.grant.PermissionsResultAction;
import com.xexun.gpstracker.msg.MsgMainFragment;
import com.xexun.gpstracker.pojo.ChatMsg;
import com.xexun.gpstracker.pojo.Device;
import com.xexun.gpstracker.pojo.MapEntity;
import com.xexun.gpstracker.receiver.TrackerReceiver;
import com.xexun.gpstracker.util.*;
import com.xexun.gpstracker.view.EmoticonsEditText;
import com.xexun.gpstracker.vo.ContType;
import com.xexun.gpstracker.vo.FlavorType;
import com.xexun.gpstracker.vo.Session;
import com.xexun.gpstracker.vo.TermType;

public class ChatActivity extends BaseActivity implements ChatTerMianlAddOneFragment.ChatAddOneFragmentCallBack,
        ChatTerMainEmojiFragment.ChatTerMainEmojiFragmentCallBack {

    private String TAG = ChatActivity.class.getSimpleName();

    private static int MIX_TIME = 1; // 最短录制时间，单位秒，0为无时间限制，建议设为1
    private static int RECORD_NO = 0; // 不在录音
    private static int RECORD_ING = 1; // 正在录音
    private static int RECODE_ED = 2; // 完成录音
    private static int RECODE_STATE = 0; // 录音的状态
    private static float recodeTime = 0.0f; // 录音的时间
    private static double voiceValue = 0.0; // 麦克风获取的音量值
    private ImageView dialog_img;
    private ProgressBar dialog_pro;//录音进度条
    private Dialog dialog;
    private AudioRecorder mr;
    private Thread recordThread;
    private boolean hasVoice = false, isInVoice = false;

    //-------------------

    private Context mContext = ChatActivity.this;
    private CameraUtil mCameraUtil;

    private ImageButton backButton, locationButton, expressionButton, phoneButton;
    private LinearLayout chatAddSelect, selectImageLayout, selectVideoLayout, selectCameraLayout;
    MapEntity mEntity;
    private EmoticonsEditText messageText;
    private TextView titleView;
    private ViewPager mViewPager, mSelectEmoji;
    private LinearLayout mTabLineLLayout, mTablineEmojiLayout;
    private ImageView mTabLineIv, mTabLineEmojiIv;
    private List<Fragment> mFragmentAddList = new ArrayList<>();
    //	private LinearLayout playLayout;
//	private TextView userNameView;
//	private ImageView playView;
    private ListView listView;
    private Button sendButton, voiceBar,alarmBtn;
    private boolean selecState = true;  //+号的点击状态
    private boolean selecEmojiState = true;  //表情的点击状态
    private int currentIndex;   // ViewPager的当前选中页
    private int screenWidth;    //屏幕的宽度
    public static String mEntityImei;   //IMEI
    protected static Device device;     //Device
    public static List<String> mEmoticonsNewGifCopy = new ArrayList<>();
    public static List<String> mEmoticons_ZgifCopy = new ArrayList<>();
    private InputMethodManager imm; //输入法管理器
    private SharedPreferences mSharedPreferences;

    private long lastSendTime;

    private ListViewDialog listViewDialog;
    /**
     * 选择照片
     */
    private final static int OPEN_GALLERY = 799;

    private List<ChatMsg> messages = new ArrayList<>();
    //消息
    private List<ChatMsg> adapterlist = new ArrayList<>();
    private MessageViewAdapter mAdapter;
    private String folderName = "";    //文件夹的名称
    private long amrName;//语音文件的名称


    private Messenger messenger;
    private static ChatActivity chatActivity = null;
    private ChatHandler chatHandler = new ChatHandler(new WeakReference<>(this));

    public static boolean isActive = false;
    public final static int UPDATE_CHAT_UI = 1;
    public final static int UPDATE_CHAT_FAILED = 2;
    public final static int SEND_CHAT_COUNT = 3;

    //报警列表
    private MyPullUpListView warningListView;
    private Boolean hasNext;
    private String startStr;
    private String endStr;
    private int page = 1;
    private List<Warning> list;
    private WarningListAdapter warningApater;

    public static ChatActivity getChatActivity() {
        return chatActivity;
    }

    public ChatHandler getHandler() {
        return chatHandler;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isActive = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isActive = true;
        //关闭消息提示
        TrackerReceiver.clearNotification(99);
        //隐藏底部菜单界面
        hideFootMenu();
        selecState = true;
//       initNewMsg(intent);
        if (getIntent().getBooleanExtra("addOn", false)) {
            openBottomTools();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        isActive = false;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initNewMsg(intent);
    }

    @Override
    protected void onDestroy() {
        mEntityImei = "";
        device = null;
        isActive = false;
//        if (serviceConn != null) {
//            try {
//                unbindService(serviceConn);
//            } catch (Exception e) {
//                //
//            }
//        }

        AppContext.getEventBus().unregister(this);
        super.onDestroy();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chatActivity = this;

        //zwd注释判断
//        //对于已退出的，需要重新登录
//        if (CommUtil.isEmpty(Session.getInstance().getUserList())) {
//            logout(false);
//            finish();
//            return;
//        }
//        ViewUtils.inject(this);
        //Android 5.0之后需要采用显式意图启动Service,旧有方法为隐式意图
        // bindService(new Intent(Constants.ACTION_COMM_SERVICE), serviceConn, BIND_AUTO_CREATE);
//        Intent intent = new Intent().setPackage(Constants.PACKAGER);
//        bindService(intent, serviceConn, BIND_AUTO_CREATE);
        mCameraUtil = new CameraUtil(this);

        mSharedPreferences = Utils.getPreferences(mContext);

        AppContext.getEventBus().register(this);

        setContentView(R.layout.activity_chat);

        mEntity = (MapEntity) getIntent().getSerializableExtra("chat");

        if (mEntity == null) {
            mEntityImei = getIntent().getStringExtra("IMEI");
            if (CommUtil.isNotBlank(mEntityImei)) {
                mEntity = Session.getInstance().getMapEntityByImei(mEntityImei);
            }
        } else {
            mEntityImei = mEntity.getImei();
        }
        if (mEntity == null || CommUtil.isBlank(mEntityImei)) {
            logout(false);
            finish();
            return;
        }

        //获得Device
        device = Session.getInstance().getDevice(mEntityImei);
        folderName = String.valueOf(Math.abs(mEntityImei.hashCode()) % 100);
//		 try {
//			FileUtils.saveFolder(ChatActivity.this, Utils.Md5(mEntity.getImei()+Session.getInstance().getUser().getPhone()));
//		} catch (Exception e1) {
//			e1.printStackTrace();
//		}

        backButton = (ImageButton) findViewById(R.id.backBtn);
        messageText = (EmoticonsEditText) findViewById(R.id.et_sendmessage);
        titleView = (TextView) findViewById(R.id.titleNameTV);
//        voiceButton = (ImageButton) findViewById(R.id.voiceBtn);
        phoneButton = (ImageButton) findViewById(R.id.phoneBtnChat);
        sendButton = (Button) findViewById(R.id.sendBtn);




//        表情控件
        expressionButton = (ImageButton) findViewById(R.id.expressionBtn);

        voiceBar = (Button) findViewById(R.id.voiceBar);
        chatAddSelect = (LinearLayout) findViewById(R.id.chat_add_select);
        selectImageLayout = (LinearLayout) findViewById(R.id.imageLayout);
        selectCameraLayout = (LinearLayout) findViewById(R.id.cameraLayout);
        selectVideoLayout = (LinearLayout) findViewById(R.id.videoLayout);
        listView = (ListView) findViewById(R.id.listview);
        //更多控件
//        addMorBtn = (ImageButton) findViewById(R.id.addMoreBtn);
        mViewPager = (ViewPager) findViewById(R.id.chat_add_select_vp);
        mTabLineLLayout = (LinearLayout) findViewById(R.id.chat_add_tab_line_llayout);
        mTabLineIv = (ImageView) findViewById(R.id.chat_add_tab_line_iv);

        alarmBtn = (Button) findViewById(R.id.alarmBtn);
        warningListView = (MyPullUpListView) findViewById(R.id.warning);

        titleView.setText(mEntity.getName());
        backButton.setOnClickListener(clickListener);
        expressionButton.setOnClickListener(clickListener);
        selectImageLayout.setOnClickListener(clickListener);
        selectCameraLayout.setOnClickListener(clickListener);
        selectVideoLayout.setOnClickListener(clickListener);
//        addMorBtn.setOnClickListener(clickListener);
        mViewPager.setOnClickListener(clickListener);
        phoneButton.setOnClickListener(clickListener);
        alarmBtn.setOnClickListener(clickListener);

        initCallList();



        messageText.setMovementMethod(ScrollingMovementMethod.getInstance());
            /*初始化数据 */
        mAdapter = new MessageViewAdapter(ChatActivity.this, adapterlist, mEntity.getPortrait());
        listView.setAdapter(mAdapter);

        //保存新表情数据到sp中（暂时存放在此处）
        saveEmoji();

        initData();
        initNewMsg(getIntent());
        initEvents();

        //手铐放开蓝牙功能
        //if (Constants.FLAVOR == FlavorType.HANDCUFF || Constants.FLAVOR == FlavorType.OLDGPS || Constants.TERM_TYPE_BRACELET == AppContext.termType) {
        if (Constants.FLAVOR == FlavorType.OLDGPS) {
            LinearLayout bottomLayout = (LinearLayout) findViewById(R.id.bottomLayout);
            bottomLayout.setVisibility(View.GONE);
        }
    }



      static final String WARNINGURL = Constants.baseUrl+"terminal/getContactsByTermId";


    List<PhoneNumber>phoneList = new ArrayList<PhoneNumber>();

    //初始化电话列表
    private void initCallList(){


        JSONObject jsonObj = new JSONObject();

        if(null == device){
            return;
        }

        try {
            jsonObj.put("termId",device.getId()) ;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //zwd注释，打电话功能
//        HttpUtil.post(WARNINGURL, jsonObj, new Callback.ProgressCallback<JSONObject>() {
//            @Override
//            public void onWaiting() {
//
//            }
//
//            @Override
//            public void onStarted() {
//
//            }
//
//            @Override
//            public void onLoading(long total, long current, boolean isDownloading) {
//
//            }
//
//            @Override
//            public void onSuccess(JSONObject result) {
//
//                JSONArray body = null;
//                try {
//                    body = result.getJSONArray("body");
//                    for(int i = 0 ; i< body.length();i++){
//                        JSONObject obj = (JSONObject) body.get(i);
//                        PhoneNumber phoneNumber = new PhoneNumber(obj.getString("name"),obj.getString("phone"),obj.getString("relation"));
//                        phoneList.add(phoneNumber);
//                    }
//
//                    if(phoneList.size() == 0){
//                        return;
//                    }
//
//
//                    listViewDialog = new ListViewDialog(chatActivity, phoneList,new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            Log.i(TAG,"点击了取消拨打电话按钮");
//                            listViewDialog.dismiss();
//                        }
//                    });
//                    listViewDialog.setTitle("电话");
//
//
////                    CallAdapter = new CallAdapter(phoneListView,mContext,phoneList);
////                    CallAdapter.setOnCallClickListener(new CallAdapter.OnCallClickListener() {
////                        @Override
////                        public void onClick(PhoneNumber phone, int position) {
////
////                            //打电话
////                            Intent intent = new Intent(Intent.ACTION_CALL);
////                            Uri data = Uri.parse("tel:" + phone.getCallNumber());
////                            intent.setData(data);
////                            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
////                                // TODO: Consider calling
////                                //    ActivityCompat#requestPermissions
////                                // here to request the missing permissions, and then overriding
////                                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
////                                //                                          int[] grantResults)
////                                // to handle the case where the user grants the permission. See the documentation
////                                // for ActivityCompat#requestPermissions for more details.
////                                return;
////                            }
////                            startActivity(intent);
////
////
////                        }
////                    });
////
////                    phoneListView.setAdapter(CallAdapter);
//
//
//
//
//
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                }
//
//
//            }
//
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onCancelled(Callback.CancelledException cex) {
//
//            }
//
//            @Override
//            public void onFinished() {
//
//            }
//        });


    }




    public ListView getListView() {
        return listView;
    }

    /**
     * 考虑到后期对更新表情包的位置修改，将表情数据存储到sp中,
     * 暂时放置到此处，以后可根据需求进行挪动
     */
    private void saveEmoji() {
        if (MsgMainFragment.mEmoticonsNewGif.size() != 0) {
            String newEmoji = CommUtil.join(MsgMainFragment.mEmoticonsNewGif.toArray(), ",");//Utils.listToString(MsgMainFragment.mEmoticonsNewGif);
            //保存表情数据到SP中
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putString(MyConstants.EMOJI_APP_NEW, newEmoji);
            editor.apply();
        }

    }

    private void initEvents() {
        messageText.addTextChangedListener(new TextWatcher() {
            private String bf;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                bf = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (messageText.getText().length() > 0) {
//                    addMorBtn.setVisibility(View.GONE);
//                    sendButton.setVisibility(View.VISIBLE);
//                } else {
//                    sendButton.setVisibility(View.GONE);
//                    addMorBtn.setVisibility(View.VISIBLE);
//                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mEntity != null && mEntity.getTermType() != TermType.APP && CommUtil.calcASCIILen(s.toString()) > 30) {
                    CommUtil.showMsgShort(getString(R.string.msg_too_long));
                    messageText.setText(bf);
                }
            }
        });
        messageText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                messageText.requestFocus();
                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                if (imm != null) {
                    imm.showSoftInput(messageText, 0);
                    imm.showSoftInputFromInputMethod(messageText.getWindowToken(), InputMethodManager.SHOW_FORCED);
                }
                //隐藏底部菜单界面
                hideFootMenu();
            }
        });
        messageText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    if (hasFocus) {
                        imm.showSoftInput(messageText, 0);
                        imm.showSoftInputFromInputMethod(messageText.getWindowToken(), InputMethodManager.SHOW_FORCED);
                    } else {
                        imm.hideSoftInputFromWindow(messageText.getWindowToken(), 0);
                    }
                }
                //隐藏底部菜单界面
                hideFootMenu();
            }
        });
        sendButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt = messageText.getText().toString().trim();
//                if (CommUtil.isBlank(txt)) {
//                    return;
//                }


                if (!TextUtils.isEmpty(txt)) {
                    sendTextMsg(txt);

                    //隐藏底部菜单界面
                    hideFootMenu();
                }else {
                    Toast.makeText(ChatActivity.this,getString(R.string.send_empty),Toast.LENGTH_SHORT).show();
                }
                // messageText.requestFocus();

            }
        });
//        voiceButton.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(ChatActivity.this, new String[]{Manifest.permission.RECORD_AUDIO},
//                        new PermissionsResultAction() {
//                            @Override
//                            public void onGranted() {
//                                toggleVoiceButton();
//                            }
//
//                            @Override
//                            public void onDenied(String permission) {
//                                CommUtil.showMsgLong(String.format(getString(R.string.permission_refuse_comm_note), getString(R.string.permission_refuse_record_audio_note)));
//                            }
//                        });
//            }
//        });
        voiceBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(voiceBar.getLayoutParams());
                params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (RECODE_STATE != RECORD_ING) {
                            //voiceBar.setBackgroundResource(R.drawable.chat_content_edit_dark);
                            voiceBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.chat_content_edit_dark));
                            voiceBar.setLayoutParams(params);
                            String sdState = Environment.getExternalStorageState();// 获得sd卡的状态
                            if (!sdState.equals(Environment.MEDIA_MOUNTED)) { // 判断SD卡是否存在
                                // 提示sd卡不存在
                                Toast.makeText(mContext, R.string.media_mounted_no, Toast.LENGTH_SHORT).show();
                                return false;
                            }
//                            deleteOldFile();
                            amrName = System.currentTimeMillis();
                            mr = new AudioRecorder(ChatActivity.this, folderName, amrName + "");
                            RECODE_STATE = RECORD_ING;
                            showVoiceDialog();
                            try {
                                mr.start();
                            } catch (Exception e) {
                                // FIXME 在华为cxxxx上会崩溃
                                e.printStackTrace();
                            }
                            recordThread = new Thread(RecordThread);
                            recordThread.start();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if (RECODE_STATE == RECORD_ING) {
                            RECODE_STATE = RECODE_ED;
//                            voiceBar.setBackground(getResources().getDrawable(R.drawable.chat_content_edit,null));
                            voiceBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.chat_content_edit));
                            voiceBar.setLayoutParams(params);
                            if (dialog != null && dialog.isShowing()) {
                                dialog.dismiss();
                            }
                            try {
                                if (mr != null) {
                                    mr.stop();
                                }
                                voiceValue = 0.0;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            if (recodeTime < MIX_TIME) {
                                showWarnToast();
//								record.setText("按住开始录音");
                                RECODE_STATE = RECORD_NO;
                            } else {
                                // TO-DO
//								record.setText("录音完成!点击重新录音");
                                hasVoice = true;
                                sendVoice();
                            }
                        }
                        break;
                }
                return false;
            }
        });
    }

    public void toggleVoiceButton() {
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (isInVoice) {
            messageText.setVisibility(View.VISIBLE);
//            voiceButton.setImageResource(R.drawable.chat_icon_voice);
            voiceBar.setVisibility(View.GONE);
            messageText.requestFocus();
            imm.showSoftInput(messageText, 0);
            imm.showSoftInputFromInputMethod(messageText.getWindowToken(), InputMethodManager.SHOW_FORCED);
            isInVoice = false;
        } else {
//            voiceButton.setImageResource(R.drawable.keyboard);
            messageText.setVisibility(View.GONE);
            voiceBar.setVisibility(View.VISIBLE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow(messageText.getWindowToken(), 0);
            }
            isInVoice = true;
        }
    }


    private void sendTextMsg(String txt) {
        messageText.setText(null);
        ChatMsg chatMsg = new ChatMsg();
        chatMsg.setIsSend(true);
        chatMsg.setImei(mEntity.getImei());
        chatMsg.setType(ContType.TXT.getType());
        chatMsg.setTermType(mEntity.getTermType().getType());
        chatMsg.setContent(txt);
        chatMsg.setUserId(Session.getInstance().getLoginedUserId());
        chatMsg.setTime(System.currentTimeMillis());
        sendMsg(chatMsg);
    }

    public void hideFootMenu() {
        chatAddSelect.setVisibility(View.GONE);
        mViewPager.setVisibility(View.GONE);
        mTabLineLLayout.setVisibility(View.GONE);
    }

    @Subscriber(tag = Constants.EVENT_TAG_CHAT_DISPLAYMSG)
    private void displayMsg(ChatMsg chatMsg) {
        if (mEntityImei != null && mEntityImei.equals(chatMsg.getImei())) {
            messages.add(chatMsg);
            mAdapter.notifyDataSetChanged();
        }
    }

    public void sendMsg(ChatMsg chatMsg) {
        if (System.currentTimeMillis() - lastSendTime < 1000) {
            CommUtil.showMsgShort(getString(R.string.too_fast));
            return;
        }
        lastSendTime = System.currentTimeMillis();
        adapterlist.add(chatMsg);
        mAdapter.notifyDataSetChanged();

        try {
            AppContext.db.saveBindingId(chatMsg);
        } catch (Exception e) {
            Log.e(Constants.TAG, "保存聊天信息到数据库失败！", e);
        }
        AppContext.getEventBus().post(chatMsg, Constants.EVENT_TAG_CHAT_SEND);
    }

    /**
     * 初始化聊天记录
     */
    private void initData() {
        try {
            long userId = Session.getInstance().getLoginedUserId();
            if (userId == 0) {
                return;
            }
            markReaded();
            List<ChatMsg> msgs = AppContext.db.selector(ChatMsg.class).where("user_id", "=", userId)
                    .and("imei", "=", mEntity.getImei()).orderBy("time", true).limit(30).findAll();

            //排序

            if(msgs.size() > 1){


                for(int i = 0; i < msgs.size() - 1; i++){
                    for(int j = 0; j < msgs.size() - i - 1; j++){
                        ChatMsg chatMsg1 = msgs.get(j);
                        ChatMsg chatMsg2 = msgs.get(j + 1);
                        Log.e("Message:",chatMsg1.getContent());
                        String timeStr1 = "";
                        String timeStr2 = "";
                        String  time1 = "";
                        String time2 = "";
                        if(chatMsg1.getIsSend()){
                            time1 = CommUtil.getDateTime(new Date(chatMsg1.getTime()));
                        }else{
                            timeStr1 = chatMsg1.getContent();
                            time1 =   timeStr1.substring(timeStr1.indexOf("(")+1,timeStr1.indexOf(")"));
                        }


                        if(chatMsg2.getIsSend()){
                           time2 = CommUtil.getDateTime(new Date(chatMsg2.getTime()));
                        }else {
                            timeStr2 = chatMsg2.getContent();
                            time2 =  timeStr2.substring(timeStr2.indexOf("(")+1,timeStr2.indexOf(")"));
                        }

                        //判断含有 / 的字符，就是英文日期格式化
                        boolean contains = time1.contains("/");
                        SimpleDateFormat simpleDateFormat1 = null;
                        if (contains){
                            simpleDateFormat1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
                            Log.e("Message formate1 :", "有斜杠");
                        }else {
                            simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Log.e("Message formate1 :", "无斜杠");
                        }

                        boolean contains2 = time2.contains("/");
                        SimpleDateFormat simpleDateFormat2 = null;
                        if (contains2){
                            simpleDateFormat2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
                        }else {
                            simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        }

                        Date date1 = simpleDateFormat1.parse(time1);
                        Date date2 = simpleDateFormat2.parse(time2);


                        if(date1.after(date2)){

                            ChatMsg chatMsg = chatMsg1;

                            int t = msgs.indexOf(chatMsg1);
                            msgs.set(t,chatMsg2);
                            msgs.set(t+1,chatMsg);

                        }
                    }
                }
            }

            //去重
            if(msgs.size() > 0) {

                for (int i = 0; i < msgs.size() - 1; i++) {
                    for (int j = msgs.size() - 1; j > i; j--) {
                        ChatMsg chatMsg = msgs.get(j);
                        ChatMsg chatMsg2 = msgs.get(i);
                        if (chatMsg.getIsSend() || chatMsg2.getIsSend()) {
                            continue;
                        }

                        if (chatMsg.getContent().equals(chatMsg2.getContent())) {
                            msgs.remove(j);
                        }
                    }
                }

            }


           // st1.substring(st1.indexOf("(")+1,st1.indexOf(")"))


            adapterlist.addAll(msgs);
            Collections.reverse(msgs);
            messages.addAll(msgs);
            mAdapter.notifyDataSetChanged();
        } catch (Exception e) {
//            e.printStackTrace();
            Log.i(TAG,"日期解析报错....");
        }
    }

    /**
     * 修改当前用户的当前好友的消息为已读
     */
    private void markReaded() {
        try {
            long userId = Session.getInstance().getLoginedUserId();
            int update = AppContext.db.update(ChatMsg.class, WhereBuilder.b("user_id", "=", userId).and("imei", "=", mEntity.getImei()), new KeyValue("is_read", 1));
            Log.e(TAG,"更新数量update为:"+update);

            Utils.stopSound();

            //更新桌面未读条数
            checkNewMeaasge();

        } catch (DbException e) {
            Log.e(TAG,"修改当前用户的当前好友的消息为已读失败");
            e.printStackTrace();
        }
    }

    public void checkNewMeaasge() {

        Map<String, Integer> unreadData = new HashMap<>();
        int unReadCount = 0;
        //获取数据库数据
        try {
            //查询未读消息的个数以及对应的IMEI
//          SqlInfo sqlInfo = new SqlInfo("select imei,count(is_read) unRead from chat_msg where is_read=? and user_id=? group by imei");

            List<Device> devices = Session.getInstance().getMonitors();

            if(devices.size() == 0 ){
                return;
            }

            for (Device device : devices){
                SqlInfo sqlInfo = new SqlInfo("select imei,count(is_read) unRead from chat_msg where is_read=? and imei=? and user_id=? group by imei");
                sqlInfo.addBindArg(new KeyValue("is_read", 0));
                sqlInfo.addBindArg(new KeyValue("imei", device.getImei()));
                sqlInfo.addBindArg(new KeyValue("user_id", Session.getInstance().getLoginedUserId()));
//              sqlInfo.addBindArg(new KeyValue("user_id", Session.getInstance().getLoginedUserId()));
                List<DbModel> dbModels = AppContext.db.findDbModelAll(sqlInfo);

                Log.d(TAG, "未读数据总数标记：（1或以上代表有未读数据，0代表没有）：" + dbModels.size());

                for (DbModel dbModel : dbModels) {
                    HashMap<String, String> dataMap = dbModel.getDataMap();
                    int unRead = Integer.parseInt(dataMap.get("unRead"));
                    unReadCount += unRead;
                    String imei = dataMap.get("imei");
                    unreadData.put(imei, unRead);
                }
            }

            Log.d(TAG,"未读消息条数："+unReadCount);

            //清除旧提示
            if (unReadCount != 0) {
                if (!Build.MANUFACTURER.equalsIgnoreCase(MobileBrand.XIAOMI)) {
                    BadgeNumberManager.from(ChatActivity.this).setBadgeNumber(unReadCount);
                }
            } else {
                BadgeNumberManager.from(ChatActivity.this).setBadgeNumber(0);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    /**
     * 点击事件
     */
    private OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.backBtn:
                    hideSoftInputWindow();
                    ChatActivity.this.finish();
                    break;
//			case R.id.et_sendmessage:
//
//				break;
//			case R.id.playIV:
//				downloadVoice(mEntity.getVoiceUrl());
//				break;
                case R.id.imageLayout:
                    //选择图片
                    Intent intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent, OPEN_GALLERY);
                    break;
                case R.id.cameraLayout:
                    mCameraUtil.openCamera();
                    break;
//                case R.id.addMoreBtn:
//                    //点击+号按钮
//                    if (selecState) {
////                        openBottomTools();
//                        chatAddSelect.setVisibility(View.VISIBLE);
//                        hideSoftInputWindow();
//                        selecState = !selecState;
//                    } else {
//                        chatAddSelect.setVisibility(View.GONE);
//                        selecState = !selecState;
////                        //隐藏选择界面
////                        hideFootMenu();
////                        //清除底部菜单数据
////                        mFragmentAddList.clear();
////                        mViewPager.removeAllViews();
////
////                        selecState = !selecState;
//
//                    }
//                    break;
                case R.id.expressionBtn:
                    //表情
                    Log.d(Constants.TAG, "这里是点的了");
                    selectEmojis();
                    break;
                case R.id.phoneBtnChat:
                    onPhoneButtonClick(v);
                    break;
                case R.id.alarmBtn:
                    if(warningListView.getVisibility() == View.VISIBLE){
                        warningListView.setVisibility(View.GONE);
                    }else{
                        ShowDateDialog();
                    }

                    break;
            }
        }
    };

    public void openBottomTools() {
        viewPagerClear();
        //加载底部菜单数据
        footTerMinalMenuData();
        if (mEntity.getTermType().equals(TermType.APP)) {
            //非终端数据，在此指代监护人数据(以后如果涉及的功能增多，一页显示不完，可以使用Fragment的方法显示，可参考ChatTerMianlAddOneFragment.java)
            mViewPager.setVisibility(View.GONE);
            mTabLineLLayout.setVisibility(View.GONE);
            chatAddSelect.setVisibility(View.VISIBLE);
        } else {
            //显示选择界面
            chatAddSelect.setVisibility(View.GONE);
            mViewPager.setVisibility(View.VISIBLE);
            mTabLineLLayout.setVisibility(View.VISIBLE);
        }
        selecState = !selecState;
        selecEmojiState = true;
        hideSoftInputWindow();
    }

    private void selectEmojis() {
        if (selecEmojiState) {
            hideSoftInputWindow();
            //清除底部菜单数据
            viewPagerClear();
            //清除变量数据
            mEmoticonsNewGifCopy.clear();
            mEmoticons_ZgifCopy.clear();
            //设置中间数据
            String newEmoji = mSharedPreferences.getString(MyConstants.EMOJI_APP_NEW, "");
            if (!"".equals(newEmoji)) {
                String[] split = newEmoji.split(",");
                Collections.addAll(mEmoticonsNewGifCopy, split);
            } else {
                //没有新表情数据，则加载APP自带的表情
                String APPEmoji = mSharedPreferences.getString(MyConstants.EMOJI_APP_OLD, "");
                if (!"".equals(APPEmoji)) {
                    String[] split = APPEmoji.split(",");
                    Collections.addAll(mEmoticons_ZgifCopy, split);
                }
            }
            int tab;
            if (mEmoticonsNewGifCopy.size() != 0) {
                tab = mEmoticonsNewGifCopy.size() % 8 == 0 ? mEmoticonsNewGifCopy.size() / 8 : mEmoticonsNewGifCopy.size() / 8 + 1;
            } else {
                tab = mEmoticons_ZgifCopy.size() % 8 == 0 ? mEmoticons_ZgifCopy.size() / 8 : mEmoticons_ZgifCopy.size() / 8 + 1;
            }
            //根据Tab个数，来设置有几个fragment界面
            for (int i = 0; i < tab; i++) {
                ChatTerMainEmojiFragment emojiFragment = ChatTerMainEmojiFragment.newInstance(i + 1, tab);
                mFragmentAddList.add(emojiFragment);
            }
            bottomViewPager(mFragmentAddList);
            //设置输入框
            messageText.setVisibility(View.VISIBLE);
//            voiceButton.setImageResource(R.drawable.chat_icon_voice);
            voiceBar.setVisibility(View.GONE);
            //显示选择界面
            mViewPager.setVisibility(View.VISIBLE);
            mTabLineLLayout.setVisibility(View.VISIBLE);

            selecState = true;
            selecEmojiState = !selecEmojiState;
        } else {
            //隐藏选择界面
            hideFootMenu();
            //清除底部菜单数据
            mFragmentAddList.clear();
            mViewPager.removeAllViews();

            selecEmojiState = !selecEmojiState;
        }
        if (chatAddSelect.isShown()) {
            chatAddSelect.setVisibility(View.GONE);
        }
    }

    private void viewPagerClear() {
        //清除底部菜单数据
        mFragmentAddList.clear();
        mViewPager.removeAllViews();
    }

    private void hideSoftInputWindow() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(messageText.getWindowToken(), 0);
    }


    /**
     * 终端底部“+”菜单
     */
    public void footTerMinalMenuData() {

        //.beginTransaction()
        //获得Fragment对象
        ChatTerMianlAddOneFragment mChatTerMianlAddOneFragment = ChatTerMianlAddOneFragment.newInstance();
        mFragmentAddList.add(mChatTerMianlAddOneFragment);

        if (Constants.FLAVOR == FlavorType.FULL) {
            ChatTerMianlAddTwoFragment mChatTerMianlAddTwoFragment = ChatTerMianlAddTwoFragment.newInstance();
            mFragmentAddList.add(mChatTerMianlAddTwoFragment);
        }
        bottomViewPager(mFragmentAddList);
    }

    /**
     * 底部ViewPager，显示“+”菜单和表情
     */
    private void bottomViewPager(final List<Fragment> fragmentLists) {
        final int tabNum = fragmentLists.size();
        //设置滑动条
        initTabLineWidth(tabNum);
        //创建Fragment
        FragmentManager fragmentManager = getFragmentManager();
        FragmentAdapter mFragmenAdapter = new FragmentAdapter(fragmentManager, fragmentLists);
        mViewPager.setAdapter(mFragmenAdapter);
        //默认显示第一个Fragment
        mViewPager.setCurrentItem(0);

        //滑动切换
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * @param position  当前页面，及你点击滑动的页面
             * @param offset    当前页面偏移的百分比
             * @param offsetPixels  当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float offset, int offsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv.getLayoutParams();
                /**
                 * 利用currentIndex(当前所在页面)和position(下一个页面)以及offset来
                 * 设置mTabLineIv的左边距 滑动场景：
                 * 记2个页面,
                 * 从左到右分别为0,1
                 * 0->1; 1->0
                 */
                if (currentIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 2) + currentIndex * (screenWidth / 2));

                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    lp.leftMargin = (int) (-(1 - offset) * (screenWidth * 1.0 / 2) + currentIndex * (screenWidth / 2));

                }
                /*如果有第三个界面，直接使用下面代码即可*/
                else if (currentIndex == 1 && position == 1 && tabNum == 3) // 1->2
                {
                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 3) + currentIndex * (screenWidth / 3));
                } else if (currentIndex == 2 && position == 1 && tabNum == 3) // 2->1
                {
                    lp.leftMargin = (int) (-(1 - offset) * (screenWidth * 1.0 / 3) + currentIndex * (screenWidth / 3));
                }
                mTabLineIv.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                currentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //滑动状态， 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
            }
        });
    }

    /**
     * 设置滑动条的宽度为屏幕的1/2(根据Tab的个数而定)
     */
    private void initTabLineWidth(int tab) {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv.getLayoutParams();
        lp.width = screenWidth / tab;
        mTabLineIv.setLayoutParams(lp);
    }

    @Override
    public void selectData(Intent data) {
        if (data != null && CommUtil.isNotBlank(data.getData())) {
            Bitmap bitmap = null;
            try {
                bitmap = MediaUtil.scaleToSettingSize(data.getData());
            } catch (Exception e) {
                Log.e(Constants.TAG, "读取文件失败！", e);
            }

            if (bitmap == null) {
                Toast.makeText(ChatActivity.this, R.string.send_pic_failed, Toast.LENGTH_SHORT).show();
                return;
            }
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bout);

            ChatMsg chatMsg = new ChatMsg();
            chatMsg.setIsSend(true);
            chatMsg.setImei(mEntity.getImei());
            chatMsg.setType(ContType.PIC.getType());
            chatMsg.setTermType(mEntity.getTermType().getType());
            chatMsg.setUserId(Session.getInstance().getLoginedUserId());
            chatMsg.setTime(System.currentTimeMillis());

            try {
                String path = FileUtils.saveFile(this, folderName, System.currentTimeMillis() + ".jpg", bout.toByteArray());
                chatMsg.setLocalUrl(path);
            } catch (IOException e) {
                Log.e(Constants.TAG, "保存图片失败！", e);
            }
            sendMsg(chatMsg);
        }
    }

    @Override
    public void emojiData(String emojiFlag) {
        if (emojiFlag != null && CommUtil.isNotBlank(emojiFlag)) {
            sendTextMsg(emojiFlag);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Constants.CAMERA_REQUEST_CODE) { //拍照完成后
            Log.d(Constants.TAG, "拍照完成");

            Bitmap bitmap = null;
            String filePath = mCameraUtil.getFilePath();
            if (CommUtil.isNotBlank(filePath)) {
                bitmap = MediaUtil.scaleToSettingSize(filePath);
            }
            if (null != bitmap) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bout);
                Log.d(Constants.TAG, "图片的大小：：：：：" + bout.toByteArray().length / 1024);

                ChatMsg chatMsg = new ChatMsg();
                chatMsg.setIsSend(true);
                chatMsg.setImei(mEntity.getImei());
                chatMsg.setType(ContType.PIC.getType());
                chatMsg.setTermType(mEntity.getTermType().getType());
                chatMsg.setUserId(Session.getInstance().getLoginedUserId());
                chatMsg.setTime(System.currentTimeMillis());
                try {
                    String path = FileUtils.saveFile(this, folderName, System.currentTimeMillis() + ".jpg", bout.toByteArray());
                    chatMsg.setLocalUrl(path);
                } catch (IOException e) {
                    Log.e(Constants.TAG, "保存图片失败！", e);
                }
                sendMsg(chatMsg);
            } else {
                Log.d(Constants.TAG, "photo为空！！！");
            }

        }

        if (data != null && CommUtil.isNotBlank(data.getData())) { //选取图片发送
            Bitmap bitmap = null;
            try {
                bitmap = MediaUtil.scaleToSettingSize(data.getData());
            } catch (Exception e) {
                Log.e(Constants.TAG, "读取文件失败！", e);
            }

            if (bitmap == null) {
                Toast.makeText(ChatActivity.this, R.string.send_pic_failed, Toast.LENGTH_SHORT).show();
                return;
            }
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bout);

            ChatMsg chatMsg = new ChatMsg();
            chatMsg.setIsSend(true);
            chatMsg.setImei(mEntity.getImei());
            chatMsg.setType(ContType.PIC.getType());
            chatMsg.setTermType(mEntity.getTermType().getType());
            chatMsg.setUserId(Session.getInstance().getLoginedUserId());
            chatMsg.setTime(System.currentTimeMillis());

            try {
                String path = FileUtils.saveFile(this, folderName, System.currentTimeMillis() + ".jpg", bout.toByteArray());
                chatMsg.setLocalUrl(path);
            } catch (IOException e) {
                Log.e(Constants.TAG, "保存图片失败！", e);
            }
            sendMsg(chatMsg);
        }
//		}
    }


    // 删除老文件
    private void deleteOldFile() {
        File file = new File(Utils.getAvailableStoragePath(), "gpstracker/voice.amr");
        if (file.exists()) {
            file.delete();
        }
    }

    // 录音时显示麦克风图片的Dialog
    protected void showVoiceDialog() {
        dialog = new Dialog(ChatActivity.this, R.style.DialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.setContentView(R.layout.my_dialog);
        dialog_img = (ImageView) dialog.findViewById(R.id.dialog_img);
        dialog_pro = (ProgressBar) dialog.findViewById(R.id.dialog_pro);
        dialog_pro.setMax(Constants.VOICE_MAX_TIME * 10);
        dialog.show();
    }

    // 录音时间太短时Toast显示
    void showWarnToast() {
        //deleteOldFile();
        Toast toast = new Toast(mContext);
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setPadding(20, 20, 20, 20);

        // 定义一个ImageView
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(R.drawable.voice_to_short); // 图标

        TextView mTv = new TextView(mContext);
        mTv.setGravity(0x01);
        mTv.setText(R.string.recording_fail);
        mTv.setTextSize(14);
        mTv.setTextColor(Color.WHITE);// 字体颜色
        // mTv.setPadding(0, 10, 0, 0);

        // 将ImageView和ToastView合并到Layout中
        linearLayout.addView(imageView);
        linearLayout.addView(mTv);
        linearLayout.setGravity(Gravity.CENTER);// 内容居中
        linearLayout.setBackgroundResource(R.drawable.record_bg);// 设置自定义toast的背景

        toast.setView(linearLayout);
        toast.setGravity(Gravity.CENTER, 0, 0);// 起点位置为中间 100为向下移100dp
        toast.show();
    }

    // 录音线程
    private Runnable RecordThread = new Runnable() {

        @Override
        public void run() {
            recodeTime = 0.0f;
            while (RECODE_STATE == RECORD_ING) {
                if (recodeTime >= Constants.VOICE_MAX_TIME && Constants.VOICE_MAX_TIME != 0) {
                    //System.out.println("超过时间了");
                    imgHandle.sendEmptyMessage(0);
                } else {
                    try {
                        Thread.sleep(200);
                        recodeTime += 0.2;
                        if (RECODE_STATE == RECORD_ING) {
                            voiceValue = mr.getAmplitude();
                            imgHandle.sendEmptyMessage(1);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        Handler imgHandle = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                switch (msg.what) {
                    case 0:
                        // 录音超过15秒自动停止
                        if (RECODE_STATE == RECORD_ING) {
                            RECODE_STATE = RECODE_ED;
                            voiceBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.chat_content_edit));
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(voiceBar.getLayoutParams());
                            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                            voiceBar.setLayoutParams(params);
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                                //更改 录音超时Toast @author zhongjiayuan
//                                Toast timeOuToast = Toast.makeText(ChatActivity.this, "录音超时,自动发送", Toast.LENGTH_SHORT);
//                                timeOuToast.setGravity(Gravity.CENTER, 0, 0);
//                                timeOuToast.show();
                            }
                            try {
                                mr.stop();
                                voiceValue = 0.0;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            if (recodeTime < 1.0) {
                                showWarnToast();
//									record.setText("按住开始录音");
                                RECODE_STATE = RECORD_NO;
                            } else {
//									record.setText("录音完成!点击重新录音");
                                hasVoice = true;
                                sendVoice();
                            }
                        }
                        break;
                    case 1:
                        setDialogImage();
                        setDialogPro();
                        break;

                    default:
                        break;
                }
            }
        };
    };

    //设置dialog计时进度条
    private void setDialogPro() {
        dialog_pro.setProgress((int) recodeTime * 10);
    }

    // 录音Dialog图片随声音大小切换
    void setDialogImage() {
        if (voiceValue < 200.0) {
            dialog_img.setImageResource(R.drawable.record_animate_01);
        } else if (voiceValue > 200.0 && voiceValue < 400) {
            dialog_img.setImageResource(R.drawable.record_animate_02);
        } else if (voiceValue > 400.0 && voiceValue < 800) {
            dialog_img.setImageResource(R.drawable.record_animate_03);
        } else if (voiceValue > 800.0 && voiceValue < 1600) {
            dialog_img.setImageResource(R.drawable.record_animate_04);
        } else if (voiceValue > 1600.0 && voiceValue < 3200) {
            dialog_img.setImageResource(R.drawable.record_animate_05);
        } else if (voiceValue > 3200.0 && voiceValue < 5000) {
            dialog_img.setImageResource(R.drawable.record_animate_06);
        } else if (voiceValue > 5000.0 && voiceValue < 7000) {
            dialog_img.setImageResource(R.drawable.record_animate_07);
        } else if (voiceValue > 7000.0 && voiceValue < 10000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_08);
        } else if (voiceValue > 10000.0 && voiceValue < 14000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_09);
        } else if (voiceValue > 14000.0 && voiceValue < 17000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_10);
        } else if (voiceValue > 17000.0 && voiceValue < 20000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_11);
        } else if (voiceValue > 20000.0 && voiceValue < 24000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_12);
        } else if (voiceValue > 24000.0 && voiceValue < 28000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_13);
        } else if (voiceValue > 28000.0) {
            dialog_img.setImageResource(R.drawable.record_animate_14);
        }
    }


    /**
     * 发送语音
     */
    private void sendVoice() {
        String filePath = folderName + "/" + amrName + ".amr";
        ChatMsg chatMsg = new ChatMsg();
        chatMsg.setTime(System.currentTimeMillis());
        chatMsg.setLocalUrl(filePath);
        chatMsg.setType(ContType.AUDIO.getType());
        chatMsg.setTermType(mEntity.getTermType().getType());
        chatMsg.setIsSend(true);
        chatMsg.setImei(mEntity.getImei());
        chatMsg.setUserId(Session.getInstance().getLoginedUserId());
        try {
            Long duration = MediaUtil.getAmrDuration(this.getFilesDir() + "/" + filePath);
            if (duration > 0) {
                int dura = (int) (duration.doubleValue() / 1000 + 0.5);
                if (dura > Constants.VOICE_MAX_TIME) {
                    dura = Constants.VOICE_MAX_TIME;
                }
                chatMsg.setDuration(dura);
            }
        } catch (IOException e) {
            Log.e(Constants.TAG, "获取AMR文件时长失败！", e);
        }
        sendMsg(chatMsg);
    }

    private void initNewMsg(Intent intent) {
        LinkedList<Long> ids = new LinkedList<>();
        synchronized (AppContext.newChatMsgIds) {
            ids.addAll(AppContext.newChatMsgIds);
            AppContext.newChatMsgIds.clear();
        }
        if (CommUtil.isEmpty(ids)) {
            return;
        }
        long userId = Session.getInstance().getLoginedUserId();
        if (userId == 0) {
            return;
        }
        MapEntity mapEntity = (MapEntity) intent.getSerializableExtra("chat");
        if(mapEntity == null || mapEntity.getImei() == null || mEntity == null || mEntity.getImei() == null) {
            finish();
            return;
        }
        if (!mapEntity.getImei().equals(mEntity.getImei())) {
            return;
        }
        mEntity = mapEntity;

        if (isActive) {
            markReaded();
        }
        refreshMessages(ids);
    }

    private void refreshMessages(LinkedList<Long> ids) {
        try {
            long userId = Session.getInstance().getLoginedUserId();
            List<ChatMsg> newMsgs = AppContext.db.selector(ChatMsg.class).where(WhereBuilder.b("id", "in", ids).and("user_id", "=", userId)
                    .and("imei", "=", mEntity.getImei())).orderBy("time").findAll();
            if (CommUtil.isNotEmpty(newMsgs)) {
                if (messages == null) {
                    messages = newMsgs;
                } else {
                    for (ChatMsg chatMsg : newMsgs) {
                        if (!messages.contains(chatMsg)) {
                            messages.add(chatMsg);
                        }
                    }
                    Collections.sort(messages, new Comparator<ChatMsg>() {
                        @Override
                        public int compare(ChatMsg lhs, ChatMsg rhs) {
                            return lhs.getTime().compareTo(rhs.getTime());
                        }
                    });
                }
                mAdapter.notifyDataSetChanged();
                if (isActive) {
                    TrackerReceiver.clearNotification(99);
                }
            }
        } catch (Exception e) {
            Log.e(Constants.TAG, "接收聊天消息出错", e);
            e.printStackTrace();
        }
    }

    @Subscriber(tag = Constants.EVENT_TAG_REFRESH_CHAT_MSG)
    private void refreshMessages(ChatMsg chatMsg) {
        if (messages == null || chatMsg == null || chatMsg.getId() == null) {
            return;
        }
        for (int i = messages.size() - 1; i >= 0; i--) {
            if (messages.get(i).getId().longValue() == chatMsg.getId().longValue()) {
                messages.set(i, chatMsg);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    public static class ChatHandler extends Handler {
        private ChatActivity activity;

        public ChatHandler(WeakReference<ChatActivity> activity) {
            this.activity = activity.get();
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPDATE_CHAT_UI:
                    Intent intent = (Intent) msg.obj;
                    activity.initNewMsg(intent);
                    break;
                case UPDATE_CHAT_FAILED:
                    long id = (long) msg.obj;
                    activity.markMsgFailed(id);
                    break;
                default:
                    break;
            }
        }
    }

    private void markMsgFailed(long id) {
        for (ChatMsg msg : messages) {
            if (msg.getId() == id) {
                msg.setSucc(0);
                break;
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Subscriber(tag = Constants.EVENT_TAG_CHAT_PROGRESS)
    private void updateProgress(MessageSender.SenderMsgProgress progress) {
        int start = listView.getFirstVisiblePosition();
        int end = listView.getLastVisiblePosition();
        View view = null;
        int index = -1;
        for (int i = end; i >= start; i--) {
                 if (messages.get(i).getId().longValue() == progress.getId().longValue()) {
                view = listView.getChildAt(i - start);
                index = i;
                break;
            }
        }
        mAdapter.updateProgress(view, index, progress.getProgress());
    }

    @Subscriber(tag = Constants.EVENT_TAG_BLEANTILOSTNOTE)
    private void showBleAntiLostNote(Object obj) {
        if (obj != null && obj instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) obj;
            String cont = (String) map.get("cont");
            DialogInterface.OnClickListener listener = (DialogInterface.OnClickListener) map.get("listener");
            Utils.showNotifyDialog(this, getString(R.string.note), cont, getString(R.string.known), null, listener, null, 0, null);
        }
    }

    //@OnClick(R.id.phoneBtnChat)
    private void onPhoneButtonClick(View v) {

        if(phoneList.size() == 0){

            Toast.makeText(this,getString(R.string.no_phone),Toast.LENGTH_SHORT).show();
            return;
        }

        listViewDialog.show();
//       phoneListView.setVisibility(phoneListView.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);


        //打电话
        /*
        final String phoneNumber = mEntity.getPhone();
        if (TextUtils.isEmpty(phoneNumber)) {
            Toast.makeText(mContext, R.string.phone_num_null, Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Utils.isMobileNO(phoneNumber)) {
            Toast.makeText(mContext, R.string.phone_num_err, Toast.LENGTH_SHORT).show();
            return;
        }
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,
                new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE},
                new PermissionsResultAction() {
                    @Override
                    public void onGranted() {
                        Utils.showNotifyDialog(ChatActivity.this, R.string.note, R.string.call_phone_msg, R.string.known, 0, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent phoneIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
                                startActivity(phoneIntent);
                            }

                        }, null, 0, null);

                    }

                    @Override
                    public void onDenied(String permission) {
                        CommUtil.showMsgShort(String.format(getString(R.string.permission_refuse_comm_note), getString(R.string.permission_refuse_call_note)));
                    }
                });
                */
    }


    @Override
    public void onBackPressed() {
        hideSoftInputWindow();
        if (chatAddSelect.isShown() || mViewPager.isShown()) {
            hideFootMenu();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }


    static final String Msg_WARNINGURL = Constants.baseUrl+"terminal/getAlarms";


    private void ShowDateDialog(){
        Calendar c = Calendar.getInstance();

        // 最后一个false表示不显示日期，如果要显示日期，最后参数可以是true或者不用输入
        new DoubleDatePickerDialog(this, 0, new DoubleDatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear,
                                  int startDayOfMonth, DatePicker endDatePicker, int endYear, int endMonthOfYear,
                                  int endDayOfMonth) {
                //日期回调
                String textString = String.format("开始时间：%d-%d-%d\n结束时间：%d-%d-%d\n", startYear,
                        startMonthOfYear + 1, startDayOfMonth, endYear, endMonthOfYear + 1, endDayOfMonth);
                page = 1;
                String startDateString = String.format("%d-%d-%d", startYear, startMonthOfYear + 1, startDayOfMonth);
                String endDateString   = String.format("%d-%d-%d", endYear, endMonthOfYear + 1, endDayOfMonth);
                startStr = startDateString;
                endStr = endDateString;

//                               Toast.makeText(getActivity(),textString,Toast.LENGTH_LONG).show();

                JSONObject jsonObj = new JSONObject();
                try {

//                    SharedPreferences sp = Utils.getPreferences(getActivity());
//                    int mDeviceId = sp.getInt("mDevice",0) ;
//                    jsonObj.put("termId",Session.getInstance().getDevice().getId()) ;
                    jsonObj.put("imei",mEntity.getImei()) ;
                    jsonObj.put("startTime",startDateString) ;
                    jsonObj.put("endTime",endDateString) ;
                    jsonObj.put("page",0) ;

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                HttpUtil.post(Msg_WARNINGURL, jsonObj, new Callback.ProgressCallback<JSONObject>() {
                    @Override
                    public void onWaiting() {

                    }

                    @Override
                    public void onStarted() {
                        WizardAlertDialog.getInstance().showProgressDialog("正在获取报警信息...", ChatActivity.this);
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isDownloading) {

                    }

                    @Override
                    public void onSuccess(JSONObject result) {
                        WizardAlertDialog.getInstance().closeProgressDialog();

                        JSONObject body = null;
                        try {
                            body = result.getJSONObject("body");
                            final JSONArray warningArray = body.getJSONArray("data");
                            hasNext = body.getBoolean("hasNext");
                            list = new ArrayList<Warning>();

                            for(int i = 0 ; i< warningArray.length();i++){
                                JSONObject obj = (JSONObject) warningArray.get(i);
                                Warning warning = new Warning(obj.getString("alarmContentLocal"),obj.getString("uploadTime"),obj.getString("id"),obj.getInt("state"));
                                list.add(warning);
                            }

                            if(list.size() == 0){
                                Toast.makeText(ChatActivity.this,"该时间段无报警记录",Toast.LENGTH_SHORT).show();
                                return;
                            }


//                                            warningListView = new MyPullUpListView(getActivity());
//                                            warningListView.setBackgroundResource(R.color.white);
                            warningListView.setVisibility(View.VISIBLE);
//                                            listViewRL.addView(warningListView,2);
                            warningListView.initBottomView();
//                                          warningApater = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1,list);
                            warningApater = new WarningListAdapter(ChatActivity.this,list);
                            warningListView.setAdapter(warningApater);
                            warningListView.setMyPullUpListViewCallBack(new MyPullUpListView.MyPullUpListViewCallBack() {
                                @Override
                                public void scrollBottomState() {
                                    if(hasNext){
                                        //刷新

                                        JSONObject jsonObj = new JSONObject();
                                        try {
//                                            SharedPreferences sp = Utils.getPreferences(getActivity());
//                                            int mDeviceId = sp.getInt("mDevice",0) ;
                                            jsonObj.put("termId",Session.getInstance().getDevice().getId()) ;
                                            jsonObj.put("startTime",startStr) ;
                                            jsonObj.put("endTime",endStr) ;
                                            jsonObj.put("page",++page) ;

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        HttpUtil.post(Msg_WARNINGURL, jsonObj, new Callback.ProgressCallback<JSONObject>() {
                                            @Override
                                            public void onWaiting() {

                                            }

                                            @Override
                                            public void onStarted() {

                                            }

                                            @Override
                                            public void onLoading(long total, long current, boolean isDownloading) {

                                            }

                                            @Override
                                            public void onSuccess(JSONObject result) {

                                                JSONObject body = null;
                                                try {
                                                    body = result.getJSONObject("body");
                                                    final JSONArray warningArray = body.getJSONArray("data");
                                                    hasNext = body.getBoolean("hasNext");
                                                    for(int i = 0 ; i< warningArray.length();i++){
                                                        JSONObject obj = (JSONObject) warningArray.get(i);
                                                        Warning warning = new Warning(obj.getString("alarmContentLocal"),obj.getString("uploadTime"),obj.getString("id"));
                                                        list.add(warning);
                                                    }

                                                    if(list.size() == 0){
                                                        return;
                                                    }
                                                    warningApater.notifyDataSetChanged();
//                                                             warningListView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1,list));
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }


                                            }

                                            @Override
                                            public void onError(Throwable ex, boolean isOnCallback) {

                                            }

                                            @Override
                                            public void onCancelled(Callback.CancelledException cex) {

                                            }

                                            @Override
                                            public void onFinished() {

                                            }
                                        });



                                    }else{
                                        warningListView.setFooterText(getString(R.string.loaded));
                                    }




                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        WizardAlertDialog.getInstance().closeProgressDialog();
                        Toast.makeText(ChatActivity.this,"获取报警信息失败！",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(Callback.CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });




            }


        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), true).show();



    }




}
